//
//  InlineFunc.h
//  Archer
//
//  Created by Wu Wang on 12-5-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include "cocos2d.h"
#include "iconv.h"

#ifndef Archer_InlineFunc_h
#define Archer_InlineFunc_h

#define  LAYER_SHOW_FUNC(layer) \
static void Show() \
{\
cocos2d::CCScene *pNewScene = cocos2d::CCScene::node(); \
layer *pLayer = layer::node();\
pNewScene->addChild(pLayer); \
cocos2d::CCScene *pCurScene = cocos2d::CCDirector::sharedDirector()->getRunningScene();\
if(NULL == pCurScene)\
{\
cocos2d::CCDirector::sharedDirector()->runWithScene(pNewScene);}\
else\
{\
cocos2d::CCDirector::sharedDirector()->replaceScene(pNewScene);\
}\
};

inline cocos2d::CCRect GetRect(cocos2d::CCNode *pNode)
{
    cocos2d::CCRect rc;
    rc.origin = pNode->getPosition();
    rc.size = pNode->getContentSize();
    rc.origin.x -= rc.size.width *pNode->getAnchorPoint().x;
    rc.origin.y -= rc.size.height*pNode->getAnchorPoint().y;
    return rc;
}

inline void SetFlag(long &flag,long value) 
{
	flag |= 1 << value;
}
inline void ClearFlag(long &flag,long value)
{
	(flag &= ~(1 << value));
}
inline long CheckFlag(long flag,long value)
{
	return (flag & (1 << value));
}


/*
inline void SetFlag(long &flag,long value) 
{
	flag |= 1 << value;
}
inline void ClearFlag(long &flag,long value)
{
	(flag &= ~(1 << value));
}
inline long CheckFlag(long flag,long value)
{
	return (flag & (1 << value));
}


inline double multiply(cocos2d::CCPoint p1, cocos2d::CCPoint p2, cocos2d::CCPoint p0)

{
    
	return((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));	
    
}

inline bool IsLineInsert(cocos2d::CCPoint ptLine1Begin, cocos2d::CCPoint ptLine1End,
						 cocos2d::CCPoint ptLine2Begin, cocos2d::CCPoint ptLine2End)
{
	return((max(ptLine1Begin.x,ptLine1End.x)>=min(ptLine2Begin.x,ptLine2End.x))&&
           
           (max(ptLine2Begin.x,ptLine2End.x)>=min(ptLine1Begin.x,ptLine1End.x))&&
           (max(ptLine1Begin.y,ptLine1End.y)>=min(ptLine2Begin.y,ptLine2End.y))&&
           (max(ptLine2Begin.y,ptLine2End.y)>=min(ptLine1Begin.y,ptLine1End.y))&&
           (multiply(ptLine2Begin,ptLine1End,ptLine1Begin)*multiply(ptLine1End,ptLine2End,ptLine1Begin)>=0)&&
           (multiply(ptLine1Begin,ptLine2End,ptLine2Begin)*multiply(ptLine2End,ptLine1End,ptLine2Begin)>=0));
}

const long INSERTRECT_NONE    = 0;
const long INSERTRECT_LEFT    = 1;
const long INSERTRECT_TOP     = 2;
const long INSERTRECT_RIGHT   = 3;
const long INSERTRECT_BOTTOM  = 4;

inline bool IsLineInsertSquare(cocos2d::CCPoint ptLineBegin, cocos2d::CCPoint ptLineEnd, cocos2d::CCPoint ptLeftUp, cocos2d::CCPoint ptRightBottom)
{
	long ret = 0;
	if(IsLineInsert(ptLineBegin, ptLineEnd, ptLeftUp, ccp(ptRightBottom.x, ptLeftUp.y)))
	{
		SetFlag(ret, INSERTRECT_TOP);
	}
	if(IsLineInsert(ptLineBegin, ptLineEnd, ccp(ptRightBottom.x, ptLeftUp.y), ptRightBottom))
	{
		SetFlag(ret, INSERTRECT_RIGHT);
	}
	if(IsLineInsert(ptLineBegin, ptLineEnd, ptRightBottom, ccp(ptLeftUp.x, ptRightBottom.y)))
	{
		SetFlag(ret, INSERTRECT_BOTTOM);
	}
	if(IsLineInsert(ptLineBegin, ptLineEnd, ccp(ptLeftUp.x, ptRightBottom.y), ptLeftUp))	
	{
		SetFlag(ret, INSERTRECT_LEFT);
	}
    
	return ret;
}


struct LineParam
{
    float _a;
    float _b;
    float _c;
};

inline void CalLineParam(cocos2d::CCPoint pt1, cocos2d::CCPoint pt2, LineParam &outParam)
{
    float k;
    if(::abs(pt1.x - pt2.x) < 0.001)
    {
        k = 1;
    }
    else
    {
        k = (pt1.y - pt2.y) / (pt1.x - pt2.x);
    }
    
    float b = pt1.y - k*pt1.x;
    
    if(k < 0)
    {
        outParam._a = -k;
        outParam._b = 1;
        outParam._c = -b;
    }
    else
    {
        outParam._a = k;
        outParam._b = -1;
        outParam._c = b;
    }
}

// 右侧 > 0 左侧 < 0  直线上 = 0;
inline int PointLineRelation(cocos2d::CCPoint ptLine1, cocos2d::CCPoint ptLine2, cocos2d::CCPoint ptJudge)
{
    LineParam lp;
    CalLineParam(ptLine1, ptLine2, lp);
    
    return ptJudge.x * lp._a + ptJudge.y * lp._b + lp._c;
}
*/


#endif
